package com.buu.manager.service.impl;

import com.buu.common.utils.MD5Utils;
import com.buu.common.utils.TableIdGenerator;
import com.buu.manager.dao.EduDao;
import com.buu.manager.domain.EduDO;
import com.buu.manager.service.EduService;
import com.buu.system.dao.UserDao;
import com.buu.system.dao.UserRoleDao;
import com.buu.system.domain.SysUserDO;
import com.buu.system.domain.UserRoleDO;
import com.buu.system.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class EduServiceImpl implements EduService {

    @Autowired
    private EduDao eduDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleDao userRoleDao;


    @Override
    public EduDO get(Long id) {
        return eduDao.get(id);
    }

    @Override
    public EduDO getUserBySysUserId(Long sysUserId) {
        return eduDao.getUserBySysUserId(sysUserId);
    }

    @Override
    public List<EduDO> list(Map<String, Object> param) {
        return eduDao.list(param);
    }

    @Override
    public List<EduDO> getUsersByUnit(Map<String, Object> param) {
        return eduDao.getUsersByUnit(param);
    }

    @Override
    public int getUsersByUnitCount(Map<String, Object> param) {
        return eduDao.getUsersByUnitCount(param);
    }

    @Override
    public int count(Map<String, Object> map) {
        return eduDao.count(map);
    }

    @Transactional
    @Override
    public int save(EduDO eduDo) {

        SysUserDO sysUserDO = new SysUserDO();
        sysUserDO.setPassword(MD5Utils.encrypt(eduDo.getUserName(), eduDo.getPassword()));
        sysUserDO.setStatus(1);
        sysUserDO.setMobile(eduDo.getMobile());
        sysUserDO.setName(eduDo.getName());
        sysUserDO.setUsername(eduDo.getUserName());

        //登记系统用户表，用于登录，同时获取返回的用户表中的id
        userDao.save(sysUserDO);

        int roleId = -1;
        //获取最大用户id
        String maxUserId = eduDao.getMaxUserId(eduDo);
        if(StringUtils.isBlank(maxUserId) && eduDo.getType().equals("0")){
            maxUserId = "DUD0000";
            roleId = 2;  //督导角色id为2
        }else if(StringUtils.isBlank(maxUserId) && eduDo.getType().equals("1")){
            maxUserId = "TEA0000";
            roleId = 3;  //教师角色id为3
        }
        String userId = eduDo.getUserId();
        if(StringUtils.isBlank(userId)){
            //userId为空，说明为督导或老师，学生是有输入学号的
            userId = TableIdGenerator.generatorId(maxUserId, 3);
            if(eduDo.getType().equals("1")){
                roleId = 3;
            }else if(eduDo.getType().equals("0")){
                roleId = 2;
            }
        }else{
            roleId = 4; //学生id为4
        }
        eduDo.setUserId(userId);
        eduDo.setSysUserId(sysUserDO.getUserId());
        UserRoleDO userRoleDO = new UserRoleDO();
        userRoleDO.setRoleId(Long.valueOf(roleId));
        userRoleDO.setUserId(sysUserDO.getUserId());
        userRoleDao.save(userRoleDO);

        //登记教师学生信息表
        return eduDao.save(eduDo);

    }

    @Override
    public int update(EduDO obj) {
        System.out.println(obj);
        //更新系统用户表
        SysUserDO sysUserDO = new SysUserDO();
        sysUserDO.setUserId(obj.getSysUserId());
        sysUserDO.setMobile(obj.getMobile());
        sysUserDO.setUsername(obj.getUserName());
        sysUserDO.setName(obj.getName());
        userDao.update(sysUserDO);

        //更新用户档案表
        return eduDao.update(obj);
    }

    @Override
    public int remove(Long id) {
        EduDO eduDO = eduDao.get(Long.valueOf(id));
        //删除用户档案表
        eduDao.remove(id);
        //删除系统登录用户表
        return userDao.remove(eduDO.getSysUserId());
    }

    @Override
    public int batchRemove(List<Long> list) {
        //删除用户档案表
        eduDao.batchRemove(list);
        //删除用户系统登录表
        return userService.batchremove(list);
    }
}
